<?php 
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the functions file
    require_once('functions.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['listusers'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'List users'));
    } else {
        //Connect to to the database using values provided by db.php
        $db = new my_db;
         
        $dir = strip_tags(mysql_escape_string($_GET['dir']));
        template_headtag("Users list");
        template_header();
        template_left();
        echo "<div class='pageheadertext'>Users</div>";
        $db->query("SELECT * FROM login ORDER BY username");
         
        echo "<br/><div align='center'>";
        echo "<table class='table' width='100%'>";
        echo "<tr class='tableusertitle'>";
        echo "<td>Username</td>";
        echo "<td>First Name</td>";
        echo "<td>Last Name</td>";
        echo "<td><center>Mail</center></td>";
        echo "<td><center>Edit</center></td>";
        echo "<td><center>Permissions</center></td>";
        echo "</tr>";
        // keep getting the next row until there are no more to get
        while ($db->next_record()) {
            if ($rowstyle == 'evenrow') {
                $rowstyle = 'oddrow';
            } else {
                $rowstyle = 'evenrow';
            }
            // Print out the contents of each row into a table
            $id = strip_tags($db->f('id'));
            $email = strip_tags($db->f('email'));
            $username = strip_tags($db->f('username'));
            $firstname = strip_tags($db->f('firstname'));
            $lastname = strip_tags($db->f('lastname'));
            echo "<tr class='$rowstyle'><td>";
            echo $username;
            echo "</td><td>";
            echo $firstname;
            echo "</td><td>";
            echo $lastname;
            echo "</td><td>";
            echo "<center><a href='mailto:$email'><img src='images/content/mailgo.png' width='16' height='16' border='0' alt='E-mail user'/></a></center>";
            echo "</td><td>";
            echo "<center><a href='edituser.php?id=$id'><img src='images/content/edit.png' width='16' height='16' border='0' alt='Edit user'/></a></center>";
            echo "</td><td>";
            echo "<center><a href='changeuserlevel.php?id=$id'><img src='images/content/userlevel.png' width='16' height='16' border='0' alt='Change userlevel'/></a></center>";
            echo "</td></tr>";
        }
        echo "</table></div>";
        template_footer();
    }
?>
